package model.action.order_deal_action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.dap.dao.CityDAO;
import model.dap.dao.DealDAO;
import model.dap.domain.City;
import model.dap.domain.Deal;
import model.dap.domain.DealCategory;
import model.dap.mapper.CityMapper;
import model.dap.mapper.DealMapper;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import common.base.BaseAction;

@Results({
		@Result(name = "success", type = "tiles", location = "tiles.welcome"),
		@Result(name = "successDealbyCity", type = "tiles", location = "tiles.dealByCity"),
		@Result(name = "successDealbyCityAndCategory", type = "tiles", location = "tiles.dealByCityAndCategory"),
		@Result(name = "ERROR", type = "tiles", location = "tiles.error"),
		@Result(name = "errorex", type = "tiles", location = "tiles.none"),
		@Result(name = "projectinfoLoad", type = "tiles", location = "tiles.ProjectInfo"), })
@ParentPackage("master_tiles")
public class DealAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static final String COUNT_STRING = "count_string";
	private List<Deal> dealresults;
	private ArrayList<String> lstPage;
	String endTime;

	//	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	private String contextPath = null;
	private DealMapper mapDeal = new DealDAO(); // mapMastApp=mapDeal

	private int dealCategoryID;
	private int citySelectedID;
	private String sessionID;
	private String categoryselectedID;

	public String getCategoryselectedID() {
		return categoryselectedID;
	}

	public void setCategoryselectedID(String categoryselectedID) {
		this.categoryselectedID = categoryselectedID;
	}

	private String categoryName="n/a";

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String getSessionID() {
		return sessionID;
	}

	public void setSessionID(String sessionID) {
		this.sessionID = sessionID;
	}

	public int getCitySelectedID() {
		return citySelectedID;
	}

	public void setCitySelectedID(int citySelectedID) {
		this.citySelectedID = citySelectedID;
	}

	private DealCategory dealCategory = new DealCategory();

	// ======================================================================================================
	// GET, SET PROPERTY && COMMON METHODS

	public DealCategory getDealCategory() {
		return dealCategory;
	}

	public void setDealCategory(DealCategory dealCategory) {
		this.dealCategory = dealCategory;
	}

	public int getDealCategoryID() {
		dealCategoryID = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("dealCategoryID"));
		return dealCategoryID;
	}

	public void setDealCategoryID(int dealCategoryID) {
		this.dealCategoryID = dealCategoryID;
	}

	private Deal deal = new Deal();

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	// December 25, 2013 23:59:59
	// private SimpleDateFormat endDeateFormat = new
	// SimpleDateFormat("MMMMMMMM dd, yyyy HH:mm:ss");

	public List<Deal> getDealresults() {
		return dealresults;
	}

	public void setDealresults(List<Deal> dealresults) {
		this.dealresults = dealresults;
	}

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	public static String getCountString() {
		return COUNT_STRING;
	}

	public DealMapper getMapDeal() {
		return mapDeal;
	}

	public void setMapDeal(DealMapper mapDeal) {
		this.mapDeal = mapDeal;
	}

	public ArrayList<String> getLstPage() {
		return lstPage;
	}

	public void setLstPage(ArrayList<String> lstPage) {
		this.lstPage = lstPage;
	}

	/** Set the locale language */
	public DealAction() {

	}

	// ======================================================================================================
	// VIEW DEAL LIST (BY CITY = HO CHI MINH)
	@Action("/home")
	public String loadDeal() {
		loadParentDealCategory();
		getCityList2();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		contextPath = request.getContextPath();
		// Láº¥y sá»‘ trang
		try {
			
			if (request.getParameter("numberPage") != null) {
				this.setOffset(Integer.parseInt(request.getParameter("numberPage")));
			}
			this.setCount(mapDeal.countForSearchByCity(2));
			dealresults = mapDeal.searchForDealByCity(this.getItemPerPage(),
					((this.getOffset() - 1) * this.getItemPerPage()), 2);
			for(int i=0; i<dealresults.size();i++){
				if(dealresults.get(i).getCurrencyID().equals("vnd")){
				dealresults.get(i).setNewSellMoney(newMoney(dealresults.get(i).getSellPrice()+""));
				dealresults.get(i).setNewRootMoney(newMoney(dealresults.get(i).getRootpPrice()+""));
				}
				else{
					dealresults.get(i).setNewSellMoney(dealresults.get(i).getSellPrice()+"");
					dealresults.get(i).setNewRootMoney(dealresults.get(i).getRootpPrice()+"");	
				}
			}
			if(dealresults.size() == 0){
				return "errorex";
			}
			
			if (count % ItemPerPage == 0) {
				numberPage = count / ItemPerPage;
				return "errorex";
			} else {
				numberPage = count / ItemPerPage + 1;
			}
			// Store the count
			session.setAttribute(COUNT_STRING, this.getCount());
			// tao vong lap so trang
			lstPage = new ArrayList<String>();
			for (int y = 0; y < numberPage; y++) {
				lstPage.add("" + (y + 1));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
		
	}

	// ======================================================================================================
	// VIEW DEAL LIST BY DEAL CATEGORY
	@Action("/master/view-deal-by-categoryID")
	public String loadDealbyCategoryID() {
		loadParentDealCategory();
		getCityList2();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		contextPath = request.getContextPath();
		
		// ===================== SET SESSION CATEGORY ID
		//setCurrentCategoryID(dealCategoryID+"");
		
		// ===================== GET SESSION CATEGORY ID
		//String scategory = getCurrentCategoryID();
		//int icategory = Integer.parseInt(scategory);
		
		// ===================== GET SESSION CITY ID
		// ===================== BIEN LUU TRU TAM CITY ID
		sessionID = session.getAttribute("sessionID").toString();
		citySelected2 = sessionID;
		
		// ==================== BIEN LUU TRU TAM CATEGORY ID
		//sessionCategoryID = scategory;
		//session.setAttribute("sessionCategoryID", sessionCategoryID);
		
		//sessionCategoryID = session.getAttribute("sessionCategoryID").toString();
		
		categoryselectedID = String.valueOf(dealCategoryID) ;
		
		
		try {
			if (request.getParameter("numberPage") != null) {
				this.setOffset(Integer.parseInt(request
						.getParameter("numberPage")));
			}
			this.setCount(mapDeal.countForSearchByCityAndCategory(Integer.parseInt(citySelected2), dealCategoryID));
			dealresults = mapDeal.searchForDealByCategoryAndCity(
					this.getItemPerPage(),
					((this.getOffset() - 1) * this.getItemPerPage()),
					Integer.parseInt(citySelected2), dealCategoryID);
			for(int i=0; i<dealresults.size();i++){
				if(dealresults.get(i).getCurrencyID().equals("vnd")){
				dealresults.get(i).setNewSellMoney(newMoney(dealresults.get(i).getSellPrice()+""));
				dealresults.get(i).setNewRootMoney(newMoney(dealresults.get(i).getRootpPrice()+""));
				}
				else{
					dealresults.get(i).setNewSellMoney(dealresults.get(i).getSellPrice()+"");
					dealresults.get(i).setNewRootMoney(dealresults.get(i).getRootpPrice()+"");	
				}
			}
			if(dealresults.size() == 0){
				return "errorex";
			}
			if (count % ItemPerPage == 0) {
				numberPage = count / ItemPerPage;
			} else {
				numberPage = count / ItemPerPage + 1;
			}
			totalPage = "" + numberPage;

			// Store the count
			session.setAttribute(COUNT_STRING, this.getCount());
			// tao vong lap so trang
			lstPage = new ArrayList<String>();
			for (int y = 0; y < numberPage; y++) {
				lstPage.add("" + (y + 1));
			}
			this.categoryName=(mapDeal.selectCategoryName(dealCategoryID));

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		setCitySelected2(sessionID);
		return "successDealbyCityAndCategory";
	}

	// ===================================================================================================
	// SHOW DEAL LIST WHEN CHOOSE CITY
	@Action("/master/show-deal-list-by-city")
	public String showDealByCity() {
		loadParentDealCategory();
		getCityList2();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		contextPath = request.getContextPath();

		try {
			// ===================== SET SESSION CITY ID
			setCurrentCityID(getCitySelected2());	
			
			// ===================== GET SESSION CITY ID
			String scity = getCurrentCityID();
			int icity = Integer.parseInt(scity);
			
			// BIEN LUU TRU SESSION TAM THOI
			sessionID = scity;
			session.setAttribute("sessionID", sessionID);
			
			if (request.getParameter("numberPage") != null) {
				this.setOffset(Integer.parseInt(request.getParameter("numberPage")));
			}

			this.setCount(mapDeal.countForSearchByCity(icity));
			dealresults = mapDeal.searchForDealByCity(this.getItemPerPage(),
					((this.getOffset() - 1) * this.getItemPerPage()), icity);
			for(int i=0; i<dealresults.size();i++){
				if(dealresults.get(i).getCurrencyID().equals("vnd")){
				dealresults.get(i).setNewSellMoney(newMoney(dealresults.get(i).getSellPrice()+""));
				dealresults.get(i).setNewRootMoney(newMoney(dealresults.get(i).getRootpPrice()+""));
				}
				else{
					dealresults.get(i).setNewSellMoney(dealresults.get(i).getSellPrice()+"");
					dealresults.get(i).setNewRootMoney(dealresults.get(i).getRootpPrice()+"");	
				}
			}
			if(dealresults.size() == 0){
				return "errorex";
			}
			if (count % ItemPerPage == 0) {
				numberPage = count / ItemPerPage;
			} else {
				numberPage = count / ItemPerPage + 1;
			}
			totalPage = "" + numberPage;

			// Store the count
			session.setAttribute(COUNT_STRING, this.getCount());

			// tao vong lap so trang
			lstPage = new ArrayList<String>();
			for (int y = 0; y < numberPage; y++) {
				lstPage.add("" + (y + 1));
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		setCitySelected2(sessionID);
		return "successDealbyCity";
	}

	// ===================================================================================================
	// SHOW DEAL LIST (ALL)
	@Action("/all")
	public String showDealAll() {
		loadParentDealCategory();
		getCityList2();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		contextPath = request.getContextPath();
		try {
			if (request.getParameter("numberPage") != null) {
				this.setOffset(Integer.parseInt(request
						.getParameter("numberPage")));
			}
			this.setCount(mapDeal.countForSearch());
			dealresults = mapDeal.searchForDeal(this.getItemPerPage(),
					((this.getOffset() - 1) * this.getItemPerPage()));
			for(int i=0; i<dealresults.size();i++){
				if(dealresults.get(i).getCurrencyID().equals("vnd")){
				dealresults.get(i).setNewSellMoney(newMoney(dealresults.get(i).getSellPrice()+""));
				dealresults.get(i).setNewRootMoney(newMoney(dealresults.get(i).getRootpPrice()+""));
				}
				else{
					dealresults.get(i).setNewSellMoney(dealresults.get(i).getSellPrice()+"");
					dealresults.get(i).setNewRootMoney(dealresults.get(i).getRootpPrice()+"");	
				}
			}
			if(dealresults.size() == 0){
				return "errorex";
			}
			if (count % ItemPerPage == 0) {
				numberPage = count / ItemPerPage;
			} else {
				numberPage = count / ItemPerPage + 1;
			}
			totalPage = "" + numberPage;

			// Store the count
			session.setAttribute(COUNT_STRING, this.getCount());
			// tao vong lap so trang
			lstPage = new ArrayList<String>();
			for (int y = 0; y < numberPage; y++) {
				lstPage.add("" + (y + 1));
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	// ===================================================================================================
	//Show project info
	@Action("/cungdeal-info")
	public String showWebInfo() {
		loadParentDealCategory();
		getCityList2();
		return "projectinfoLoad";
	}

	public Deal getDeal() {
		return deal;
	}

	public void setDeal(Deal deal) {
		this.deal = deal;
	}
}